var base = null
var azurCtrl
var utils
var Keyboard
func _ready():
	pass
func _init():
	call_deferred("azurlineInit")
	pass
func _connect():
	pass

func azurlineInit():
	if globalData.infoDs.has("g_azurlaneBase"):
		base = globalData.infoDs["g_azurlaneBase"]
	if base != null:
		#初始化
		azurCtrl = globalData.infoDs["g_azurlane"]
		utils = globalData.infoDs["g_azurlaneUtils"]

#上传玩家阵容
func upload():
	var chas = checkChas()
	var body = {"userName": utils.getPlayerName(), "battleInfo":{"battleTime": sys.main.battleTime}, "difficult":jinJieData.nowLv, "step":sys.main.getStep(), 
	"charas":chas, "steamId": utils.getPlayerId(), "talents":[]}
	for i in sys.main.player.talentDs.values():
		body.talents.append(i.name)
	var res = azurCtrl.net.sendRequest("/azurline/new/team/submit", HTTPClient.METHOD_POST, to_json(body), 3)
	res = JSON.parse(res).result
	if res.code == "success":
		print("通关阵容上传成功!")

func checkChas():
	var chas = []
	for i in sys.main.btChas:
		if i.team == 2:continue
		var chaInfo = i.toJson()
		chaInfo["items"] = []
		for item in i.items:
			utils.dicAppend(chaInfo, "items", item.toJsonMin())
		chaInfo["statis"] = azurCtrl.getStatisByCha(i)
		chaInfo["damageData"] = i.damageData
		chas.append(chaInfo)
	return chas

var window
var teamWindow
var statisContainer
var detailWindow
#显示排行榜
func rankList():
	if window == null or not is_instance_valid(window):
		window = utils.createWindowDialog(sys.main.get_node("ui"), "碧蓝航线排行榜", Vector2(900, 700))
		var contentScroll = utils.createScrollContainer(window, Vector2(820, 600), Vector2(30, 30))
		contentScroll.name = "scroll"
		statisContainer = utils.createVBox(contentScroll, Vector2(30, 100))
		statisContainer.name = "vbox"
	for i in statisContainer.get_children():
		i.queue_free()
	var res = azurCtrl.net.sendRequest("/azurline/new/rankList", HTTPClient.METHOD_GET)
	if res == null:
		print("无返回数据")
		return
	res = parse_json(res)
	var index = 0
	for vo in res.data:
		var hbox = HBoxContainer.new()
		index += 1
		utils.createRichTextLabel("#{0}      [color=#FFA500]{1}[/color]".format({"0":index, "1":vo.userName}), hbox, Vector2(350, 50))
		utils.createRichTextLabel("[color=#C0C0C0]{2}[/color]".format({"2":vo.createTime}), hbox, Vector2(230, 50))
		utils.createRichTextLabel("难度：{diff}".format({"diff":vo.difficult}), hbox, Vector2(150, 50))
		utils.createItemButton(hbox, self, "查看", "loadTeamInfo", [vo.id], Vector2(0, 0))
		statisContainer.add_child(hbox)
	window.popup_centered()

func loadTeamInfo(id):
	var res = azurCtrl.net.sendRequest("/azurline/new/team/get?teamId=%d"%id, HTTPClient.METHOD_GET)
	if res == null:
		print("无返回数据")
		return
	res = JSON.parse(res).result.data
	var charas = res.charas
	charas = JSON.parse(charas).result
	if detailWindow == null or not is_instance_valid(detailWindow):
		detailWindow = utils.createWindowDialog(sys.main.get_node("ui"), "阵容详情", Vector2(900, 650))
		
		var contentScroll = utils.createScrollContainer(detailWindow, Vector2(820, 500), Vector2(30, 110))
		contentScroll.name = "scroll"
		var grid = utils.createGridContainer(contentScroll, 5, Vector2(30, 30))
		grid.name = "grid"

	if detailWindow.has_node("hbox") and is_instance_valid(detailWindow.get_node("hbox")):
		detailWindow.get_node("hbox").queue_free()
	var grid = detailWindow.get_node("scroll/grid")
	for i in grid.get_children():
		i.queue_free()

	var hbox = utils.createHBox(detailWindow, Vector2(30, 30))
	hbox.name = "hbox"
	var btn1 = utils.createItemButton(hbox, self, "天赋", "showTals", [res.talents], Vector2(30, 30))
	var btn2 = utils.createItemButton(hbox, self, "战斗统计", "showBattle", [charas, res.battleInfo], Vector2(30, 30))

	for i in charas:
		var vbox = utils.createVBox(grid)
		var chaBt = utils.newChaBt(vbox, i.id)
		chaBt.masCha.fromJson(i, false)
		utils.createItemButton(vbox, self, "装备", "showItems", [i.items])

	detailWindow.popup_centered()

var battleWindow
var battleBox
var battleDetailWindow
var battleDetailBox
func showBattle(charas, battleInfo):
	battleInfo = utils.toJson(battleInfo)
	if battleWindow == null or not is_instance_valid(battleWindow):
		battleWindow = utils.createWindowDialog(sys.main.get_node("ui"), "战斗统计", Vector2(1000, 600))
		var contentScroll = utils.createScrollContainer(battleWindow, Vector2(950, 480), Vector2(30, 100))
		battleBox = utils.createGridContainer(contentScroll, 6)
		var timeLabel = utils.createRichTextLabel("战斗时间：%d"%[int(battleInfo.battleTime)], battleWindow, Vector2(150, 50), Vector2(30, 30))
		timeLabel.name = "time"
		battleDetailWindow = utils.createWindowDialog(battleWindow, "战斗详情", Vector2(450, 550))
		var detailScroll = utils.createScrollContainer(battleDetailWindow, Vector2(400, 400), Vector2(20, 100))
		battleDetailBox = utils.createVBox(detailScroll)

	battleWindow.get_node("time").bbcode_text = "战斗时间：%d"%[int(battleInfo.battleTime)]
	for i in battleBox.get_children():
		i.queue_free()

	var statis = []
	for cha in charas:
		var s = cha.statis
		s["damageData"] = cha.damageData
		statis.append(s)
	statis.sort_custom(azurCtrl, "sortStatis")
	for i in statis:
		utils.createRichTextLabel("{0}".format({"0":i.name}), battleBox, Vector2(280, 50))
		utils.createRichTextLabel("[color=#FFA500]伤害{1}[/color]".format({"1":int(i.damage)}), battleBox, Vector2(160, 50))
		utils.createRichTextLabel("[color=#FF0000]承伤{2}[/color]".format({"2":int(i.hurt)}), battleBox, Vector2(160, 50))
		utils.createRichTextLabel("[color=#008000]治疗{3}[/color]".format({"3":int(i.deal)}), battleBox, Vector2(160, 50))
		utils.createRichTextLabel("[color=#F8F8FF]击杀{3}[/color]".format({"3":int(i.kill)}), battleBox, Vector2(100, 50))
		utils.createItemButton(battleBox, self, "详情", "showBattleDetail", [i.damageData])

	battleWindow.popup_centered()	

func showBattleDetail(damageData):
	for i in battleDetailBox.get_children():
		i.queue_free()
	for i in damageData.keys():
		utils.createRichTextLabel("[color=#DC143C]{name}[/color]：伤害{text}".format({"name":i, "text":int(damageData.get(i))}), battleDetailBox, Vector2(400, 50))
	battleDetailWindow.popup_centered()
	
func showTals(data):
	data = utils.toJson(data)
	if data.empty():return
	var msg = ""
	for i in data:
		msg += "   " + i
	sys.newBaseMsg("天赋", msg)

var itemWindow
var itemBox
func showItems(data):
	if data.empty():return
	if itemWindow == null or not is_instance_valid(itemWindow):
		itemWindow = utils.createWindowDialog(sys.main.get_node("ui"), "角色装备", Vector2(500, 400))
		itemBox = utils.createHBox(itemWindow, Vector2(50, 50))
	for i in itemBox.get_children():
		i.queue_free()
	for i in data:
		var item = sys.newItem(i.id)
		var itemBt = load("res://ui/item/item.tscn").instance()
		itemBt.init(item)
		item.fromJsonMin(i)
		itemBox.add_child(itemBt)

	itemWindow.popup_centered()

